package net.zhengxinyun.performance.App.service.BaseIndexService;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.CountryUnitIndicatorsEntity;
import net.zhengxinyun.performance.entity.SetTimeEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.CountryUnitScoreFilter;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.service.login.UserService;
import net.zhengxinyun.performance.service.setTime.SetTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public abstract class BaseService {


    @Autowired
    private SetTimeService setTimeService;
    @Autowired
    private UserService userService;

    //获取系统设置的当前年份。 控制取当前时间
    public String getCurrentYear() {

        SetTimeEntity currentYear = setTimeService.findByIndexClass("3");
        if (ObjectUtils.isNotBlank(currentYear)) {
            return currentYear.getStartTime();
        }
        return DateUtils.getCurrentYear();
    }


    /**
     * 获取手机号 用户信息
     * @param
     * @return
     */
    public UserFilter getUserByTel(String tel)
    {
        UserFilter filter = new UserFilter();
        filter.setUserPhone(tel);
        List<UserFilter> userFilters = userService.queryUsers(filter);
        if(ObjectUtils.isNotBlank(userFilters))
        {
            return  userFilters.get(0);
        }

        return null;
    }




    public List<CountryIndicatorsFilter> filterCityIndexs(List<CountryIndicatorsFilter> query, UserFilter userFilter)
    {

        if ("admin".equals(userFilter.getUserPermissions())) {
            return query;
        }
        List<CountryIndicatorsFilter> indictorlist = new ArrayList<>();
        String indexRight = userFilter.getCityIndex();
        if(StringUtils.isBlank(indexRight)){
            return  indictorlist;
        }
        HashMap<String, Object> indexIdMap = Util.getIndexIdMap(indexRight);
        for (CountryIndicatorsFilter item : query) {
            Object idObj = indexIdMap.get(item.getId()+"");
            if(ObjectUtils.isBlank(idObj)){
                continue;
            }
            String id1 = item.getId()+"";
            String id2 = indexIdMap.get(id1).toString();
            if(id1.equals(id2))
            {
                indictorlist.add(item);
            }

        }


        return indictorlist;
    }

    public List<TownIndicatorsFilter> filterTownIndex(List<TownIndicatorsFilter> list ,UserFilter user )
    {

        if("admin".equals(user.getUserPermissions()))
        {
            return  list;
        }

        String indexRight = user.getIndexRight();
        List<TownIndicatorsFilter> indictorlist = new ArrayList<>();
        if(StringUtils.isBlank(indexRight))
        {
            return  indictorlist;
        }
        HashMap<String, Object> indexIdMap = Util.getIndexIdMap(indexRight);
        for (TownIndicatorsFilter item : list) {
            Object idobj = indexIdMap.get(item.getId() + "");
            if (ObjectUtils.isBlank(idobj)) {
                continue;
            }
            String id1 = item.getId()+"";
            String id2 = indexIdMap.get(idobj+"")+"";
            if(id1.equals(id2))
            {
                indictorlist.add(item);
            }
        }

        return indictorlist;
    }

    //过滤县直单位指标
    public List<CountryUnitIndicatorsEntity> filterDeptIndex(List<CountryUnitIndicatorsEntity> list , UserFilter user)
    {


        if("admin".equals(user.getUserPermissions()))
        {
            return  list;
        }

        String indexRight = user.getDepartIndex();
        List<CountryUnitIndicatorsEntity> indictorlist = new ArrayList<>();
        if(StringUtils.isBlank(indexRight))
        {
            return  indictorlist;
        }

        HashMap<String,Object> map = Util.getIndexIdMap(indexRight);
        for (CountryUnitIndicatorsEntity item: list)
        {

            Object id2 = map.get(item.getId()+"");
            if(ObjectUtils.isBlank(id2)){
                continue;
            }

            String id1 = item.getId().toString();
            if(id1.equals(id2.toString()))
            {
                indictorlist.add(item);
            }

        }


        return indictorlist;
    }





}
